<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\portal\model\TypeModel;
use cmf\controller\AdminBaseController;
use think\Db;

class AdminSeriesTypeController extends AdminBaseController
{
    //产品系列分类


    //列表
    public function index(){
        $parent_res = self::getList();
        $this->assign('list',$parent_res);
        return $this->fetch();
    }

    //列表
    public function select(){
        $parent_res = self::getList();
        $this->assign('list',$parent_res);
        return $this->fetch();
    }

    //获取列表数据
    public static function getList(){
        $parent_res = Db::name('type')
            ->where(['pid'=>0])
            ->select()
            ->toArray();
        $res = Db::name('type')
            ->where(['pid'=>['<>',0]])
            ->select()
            ->toArray();
        foreach($parent_res as &$value){
            $k=0;
            foreach ($res as $value1){
                $k+=0;
                if($value['id'] == $value1['pid']){
                    $value['children'][$k] = $value1;
                    $value['children'][$k]['name'] = '&nbsp;&nbsp;&nbsp;├─'.$value1['name'];
                    $k++;
                }
            }
        }
        return $parent_res;
    }

    //添加页面
    public function add(){
        $selectCategory = Db::name('type')
            ->where(['pid'=>0])
            ->field('id,name')
            ->select()
            ->toArray();
        $this->assign('select_category', $selectCategory);
        return $this->fetch();
    }

    //添加提交
    public function addPost(){
        $data = $this->request->param();
        $typeModel = new TypeModel();
        $res = $typeModel->create($data);
        if($res){
            $this->success('添加成功!', url('AdminSeriesType/index'));
        }else{
            $this->error('失败');
        }
    }

    //编辑页面
    public function edit(){
        $id = $this->request->param('id');
        //分类
        $selectCategory = Db::name('type')
            ->where(['pid'=>0])
            ->field('id,name')
            ->select()
            ->toArray();
        $this->assign('select_category', $selectCategory);
        //内容
        $post = Db::name('type')
            ->where(['id'=>$id])
            ->find();
        $this->assign('post',$post);
        return $this->fetch();
    }

    //编辑提交
    public function editPost(){
        $data = $this->request->param();
        $typeModel = new TypeModel();
        // 显式指定更新数据操作
        $res = $typeModel->isUpdate(true)->save($data);
        if($res){
            $this->success('保存成功!', url('AdminSeriesType/index'));
        }else{
            $this->error('失败');
        }
    }

    //删除
    public function delete(){
        $id = $this->request->param('id');
        $pid = $this->request->param('pid');
        $typeModel = new TypeModel();
        if($pid == 0){
            //查找是否有子分类
            $res1 = $typeModel->where(['pid'=>$id])->count();
            if($res1 > 0){
                $this->error('存在子分类，不能删除');
            }
        }else{
            //查找是否有产品系列
            $res1 = Db::name('series')->where(['t_id'=>$id])->count();
            if($res1 > 0){
                $this->error('存在产品系列，不能删除');
            }
        }
        $res = $typeModel->where(['id'=>$id])->delete();
        if($res){
            $this->success('删除成功!');
        }else{
            $this->error('失败');
        }
    }
}
